﻿using ControlzEx.Standard;
using DataChangeTools.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace DataChangeTools.ViewModel
{
    public class ImageViewDialogViewModel : BindableBase, IDialogAware
    {

        #region 字段
        //用于保存鼠标点击时的位置
        public System.Windows.Point mousePosition = new System.Windows.Point();
        //鼠标点击时控件位置
        public System.Windows.Point contentCtlPosition = new System.Windows.Point();

        public DialogCloseListener RequestClose { get; set; }
        private ButtonResult result;
        public ButtonResult Result
        {
            get => result;
            set => SetProperty(ref result, value);
        }

        private TransformGroup transGroup;

        public TransformGroup TransGroup
        {
            get { return transGroup; }
            set => SetProperty(ref transGroup, value);
        }



        private string codeImg;

        public string CodeImg
        {
            get { return codeImg; }
            set => SetProperty(ref codeImg, value);
        }
        private string scaleStr;

        public string ScaleStr
        {
            get { return scaleStr; }
            set => SetProperty(ref scaleStr, value);
        }



        #endregion

        #region 命令
        public DelegateCommand<string> CloseDialogCommand { get; private set; }

        public DelegateCommand<System.Windows.Input.MouseEventArgs> MouseMoveCmd { get; private set; }

        public DelegateCommand<MouseWheelEventArgs> MouseWheelCmd { get; private set; }

        public DelegateCommand<MouseButtonEventArgs> MouseDownCmd { get; private set; }


        #endregion


        #region 构造函数

        //private IDialogParameters _parameters;

        public ImageViewDialogViewModel()
        {
            ScaleStr = "100";
            //_parameters = parameters;
            CloseDialogCommand = new DelegateCommand<string>(CloseDialog);
            transGroup = new TransformGroup();

            transGroup.Children.Add(new ScaleTransform());
            transGroup.Children.Add(new TranslateTransform());
            //ImgCtl.RenderTransform = transGroup;
            MouseMoveCmd = new DelegateCommand<System.Windows.Input.MouseEventArgs>(OnMouseMove);
            MouseWheelCmd = new DelegateCommand<MouseWheelEventArgs>(OnMouseWheel);
            MouseDownCmd = new DelegateCommand<MouseButtonEventArgs>(OnMouseDown);
        }

        #endregion


        #region Action方式实现
        void OnMouseWheel(MouseWheelEventArgs args)
        {
            var image = args.Source as System.Windows.Controls.Image;

            var ctl = VTHelper.GetParentObject<System.Windows.Controls.ContentControl>(image, "ContentView");

            var point = args.GetPosition(ctl);
            //滚轮滚动时控制 放大的倍数
            double scale = args.Delta * 0.002;
            ZoomImage(transGroup, point, scale);
        }

        //对控件进行缩放。
        private void ZoomImage(TransformGroup group, System.Windows.Point point, double scale)
        {
            var pointToContent = group.Inverse.Transform(point);

            ScaleTransform scaleT = group.Children[0] as ScaleTransform;
            var ScaleInt = (int)scaleT.ScaleX;
            ScaleStr = (ScaleInt * 100).ToString();
            if (scaleT.ScaleX + scale < 1) return;

            scaleT.ScaleX += scale;

            scaleT.ScaleY += scale;

            TranslateTransform translateT = group.Children[1] as TranslateTransform;

            translateT.X = -1 * ((pointToContent.X * scaleT.ScaleX) - point.X);

            translateT.Y = -1 * ((pointToContent.Y * scaleT.ScaleY) - point.Y);

            RaisePropertyChanged("TransGroup");
        }


        //鼠标点击，记录下点击的位置。
        void OnMouseDown(MouseButtonEventArgs args)
        {
            var image = args.Source as System.Windows.Controls.Image;

            var ctl = VTHelper.GetParentObject<System.Windows.Controls.ContentControl>(image, "ContentView");

            if (ctl == null)
            {

            }
            mousePosition = args.GetPosition((System.Windows.IInputElement)ctl);
            TranslateTransform tt = TransGroup.Children[1] as TranslateTransform;

            contentCtlPosition = new System.Windows.Point(tt.X, tt.Y);
        }


        //鼠标移动，每次移动与上一次的位置相加，并实时更新当前位置。
        private void OnMouseMove(System.Windows.Input.MouseEventArgs args)
        {
            if (args.LeftButton == MouseButtonState.Pressed)
            {
                var image = args.Source as System.Windows.Controls.Image;

                var ctl = VTHelper.GetParentObject<System.Windows.Controls.ContentControl>(image, "ContentView");

                var desPosition = args.GetPosition((System.Windows.IInputElement)ctl);

                TranslateTransform transform = transGroup.Children[1] as TranslateTransform;

                //控件移动位置的计算方式为：鼠标点击时位置+拖拽的长度(当前坐标-鼠标点击时的坐标).
                transform.X = contentCtlPosition.X + (desPosition.X - mousePosition.X);

                transform.Y = contentCtlPosition.Y + (desPosition.Y - mousePosition.Y);
                RaisePropertyChanged("TransGroup");
            }
        }
        #endregion



        #region 默认方法
        // 关闭弹窗的方法
        private void CloseDialog(string parameter)
        {
            // 根据参数设置弹窗结果

            if (parameter?.ToLower() == "ok")
                result = ButtonResult.OK;
            else if (parameter?.ToLower() == "cancel")
                result = ButtonResult.Cancel;

            // 关闭弹窗
            RequestClose.Invoke(result);
        }


        public bool CanCloseDialog()
        {
            return true; // 允许关闭弹窗
        }

        public void OnDialogClosed()
        {
            // 弹窗关闭时的逻辑

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            // 弹窗打开时的逻辑
            if (parameters.ContainsKey("CodeImg"))
            {
                //保存一下
                CodeImg = parameters.GetValue<string>("CodeImg");
            }

            //检测更新

        }
        #endregion

    }
}
